home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 11 / develop 11 code / The NetWork Project / Examples (Sources) / NetSim / UToy.p < prev   
Encoding:
Text File  |  1992-07-15  |  6.0 KB  |  266 lines  |  [TEXT/MPS ]

  1. UNIT UToy;
  2. {© G. Sawitzki, StatLab Heidelberg 1991}
  3. {File UToy. This is just a toy to give something to communicate}
  4. INTERFACE
  5.  
  6.     USES
  7.     MacUnits,{Types, Quickdraw, OSIntf, ToolIntf, PackIntf, SANE, }{ Standard Includes}
  8.     StdTools, Generic,NetSimGlobal,histogramunit;
  9.  
  10. CONST
  11.     cMaxToyData = 1000;
  12. TYPE
  13.     tToyPtr = ^tToyRecord;
  14.     tToyRecord = RECORD
  15.         col: RGBColor;
  16.         id: str255;
  17.         sorted:boolean;
  18.         minval,maxval:real;
  19.         mean,ssq:extended;
  20.         test_stat:extended;
  21.         nrData:integer;
  22.         data: ARRAY[1..cMaxToyData] OF real;
  23.     END;
  24.  
  25. VAR ToyRec: tToyPtr;
  26.     ToySeed:extended;
  27. PROCEDURE tToy_init(count:integer);
  28. PROCEDURE tToy_nextToy(Generator:longint;count:integer;VAR ToyStat:extended);
  29.  
  30. FUNCTION tToy_drawtoy(PROCEDURE DrawWhatToDraw): PicHandle;
  31. PROCEDURE tToy_DrawScatter;
  32.  
  33.     IMPLEMENTATION
  34.  
  35. PROCEDURE tToy_init(count:integer);
  36. VAR
  37.     i: integer;
  38. BEGIN
  39.     new(ToyRec);
  40.     ToySeed:=tickCount;    {initialize random number generator}
  41.     IF ToyRec<>NIL THEN
  42.     WITH ToyRec^ DO
  43.     BEGIN
  44.         IF count < cMaxToyData THEN nrData:=count
  45.         ELSE count:=cMaxToyData;
  46.         WITH col DO
  47.         BEGIN
  48.             red := random;
  49.             green := random;
  50.             blue := random;
  51.         END;
  52.         numtostring(random, id);
  53.         FOR i := 1 TO nrData DO
  54.         data[i] := random;
  55.         sorted:=false;
  56.     END;
  57. END;
  58.  
  59. FUNCTION CurStat:extended;
  60. VAR tempval:extended;
  61. BEGIN
  62.     IF toyrec=NIL THEN CurStat:=0
  63.     ELSE WITH ToyRec^ DO BEGIN
  64.         tempval:=(maxval-minval)/sqrt(ssq);
  65.         IF (ClassExtended(tempval) IN [SNAN,QNAN,Infinite]) THEN  SysBreakStr('bad CurStat value');
  66.         CurStat:=tempval;
  67.     END;
  68. END;
  69.  
  70. PROCEDURE getstat;
  71. VAR stat:tStatType;
  72.     i:integer;
  73. BEGIN
  74.     initStat(stat,'');
  75.     IF ToyRec<>NIL THEN
  76.     WITH ToyRec^ DO BEGIN
  77.         FOR i := 1 TO nrData DO
  78.         addstat(data[i],stat);
  79.         minval:=stat.min;
  80.         maxval:=stat.max;
  81.         mean:=stat.mean;
  82.         ssq:=stat.ssq;
  83.     END;
  84. END;
  85.  
  86. PROCEDURE tToy_nextToy(Generator:longint;count:integer;VAR ToyStat:extended);
  87.  
  88. {$IFC false}
  89. MPW Pascal does not understand type casting in CONST clause:
  90.  
  91. CONST cUNIF=longint('UNIF');
  92. #                  ?          
  93. ### pascal - Error 101 Identifier not of the appropriate class
  94. ### Error 102 Identifier not declared
  95. #                   ?         
  96. ### Error 20 Illegal symbol
  97. #--------------------------------------------------------------------------------------------------------------------------------
  98.     File "UToy.p"; Line 86
  99. #--------------------------------------------------------------------------------------------------------------------------------
  100. ### MPW Shell (3.3) - Execution of BuildProgram terminated.
  101. {$ENDC}
  102.  
  103. VAR
  104.     i: integer;
  105.     u1,u2,l,x:extended;
  106. BEGIN
  107.     IF ToyRec<>NIL THEN
  108.     WITH ToyRec^ DO BEGIN
  109.         IF count < cMaxToyData THEN nrData:=count
  110.         ELSE count:=cMaxToyData;
  111.  
  112. {$IFC False}
  113. {MPW Pascal cannot handle typecasting in case statements. So we
  114. have to use a chain of if-statements}
  115.  
  116.         case Generator of
  117.         longint('UNIF') :...;
  118. ### pascal - Error 101 Identifier not of the appropriate class
  119. ### Error 102 Identifier not declared
  120. #               ?           
  121. ### Error 20 Illegal symbol
  122. #                        ?  
  123. ### Error 142 Label type incompatible with selecting expression
  124.  
  125.         longint('GAUS') :...;
  126.         longint('Cchy') :...;
  127.         end;
  128. {$ENDC}
  129.  
  130.         IF Generator =longint('UNIF') THEN {uniform distribution on -MaxLongInt...MaxLongInt}
  131.         BEGIN
  132.             FOR i := 1 TO nrData DO BEGIN
  133.                 data[i] := round(randomX(ToySeed));
  134.             END;
  135.             sorted:=false;
  136.         END ELSE
  137.         IF Generator =longint('GAUS') THEN {standard gaussian (normal) distribution}
  138.         BEGIN
  139.             FOR i := 1 TO nrData DIV 2 DO
  140.             BEGIN 
  141.                 {get two U(0,1) random numbers}
  142.                 u1:=((round(randomX(ToySeed))/maxlongint) +1)/2;
  143.                 u2:=((round(randomX(ToySeed))/maxlongint) +1)/2;
  144.                 l:=sqrt(-2 * ln(u1));
  145.                 data[i] := l * cos(2 * pi * u2);
  146.                 data[nrData+1-i] := l * sin(2 * pi * u2);
  147.             END;
  148.             sorted:=false;
  149.         END ELSE
  150.         IF Generator =longint('Cchy') THEN {cauchy distribution}
  151.         BEGIN
  152.             FOR i := 1 TO nrData DO BEGIN
  153.                 REPEAT
  154.                     x := pi * ((((round(randomX(ToySeed)) / maxlongint) + 1) / 2) - 0.5);
  155.                     data[i] := sin(x) / cos(x);{may run into cos(x)=0!}
  156.                 UNTIL     NOT (ClassExtended(data[i]) IN [SNAN,QNAN,Infinite]) ;
  157.             END;
  158.             sorted:=false;
  159.         END ELSE Debugstr('Generator Unknown') {Noop & crash if Generator unknown} ;
  160.         getstat;
  161.         test_stat:=CurStat;
  162.         ToyStat:=test_stat;
  163.  
  164.     END ELSE ToyStat:=0;
  165. END;
  166.  
  167.  
  168. PROCEDURE tToy_SortToy;
  169.  
  170. PROCEDURE quicksort(count:integer);
  171.     {sort array a by key. count is nr of used entries in a }
  172.  
  173. VAR i:integer;
  174.  
  175. PROCEDURE sort(l,r:integer);
  176. VAR i,j    : integer;
  177.     x,w    : real;
  178.  
  179. BEGIN 
  180.     WITH ToyRec^ DO BEGIN
  181.         i    := l; j    := r;
  182.         x    := data(.(l+r) DIV    2.);
  183.         REPEAT
  184.             WHILE data(.i.) < x DO i := i + 1;
  185.             WHILE x < data(.j.) DO j := j - 1;
  186.             IF i <= j THEN
  187.             BEGIN w := data(.i.); data(.i.) := data(.j.); data(.j.) :=    w;
  188.                 i := i +    1; j :=    j - 1
  189.             END
  190.         UNTIL i >    j;
  191.  
  192.         IF l < j THEN sort(l,j);
  193.         IF i < r THEN sort(i,r);
  194.     END;
  195. END;  (* sort *)
  196.  
  197. BEGIN sort(1,count);
  198. END;   (* quicksort *)
  199.  
  200.  
  201.  
  202. BEGIN
  203.     WITH ToyRec^ DO
  204.     IF NOT sorted THEN BEGIN
  205.         quicksort(nrData);
  206.         sorted:=true;
  207.     END;
  208. END;
  209.  
  210. PROCEDURE tToy_DrawScatter;
  211. VAR i:integer;
  212.     val,range,minrange:real;
  213.     s:str255;
  214. BEGIN
  215.     WITH ToyRec^ DO BEGIN
  216.         IF sorted THEN BEGIN
  217.             minval:=data[1];
  218.             maxval:=data[nrData];
  219.         END ELSE BEGIN
  220.             minval:=maxint;
  221.             maxval:=-maxint-1;
  222.             WITH ToyRec^ DO FOR i:=1 TO nrData DO BEGIN 
  223.                 IF data[i]<minval THEN minval:=data[i];
  224.                 IF data[i]>=maxval THEN maxval:=data[i];
  225.             END;
  226.         END;
  227.         range:=maxval;range:=range-minval;IF range=0 THEN range:=1;
  228.         minrange:=minval-0.1*range;
  229.         range:=300/(range*1.2);
  230.         numtostring(round(range),s);
  231.  
  232.  
  233.         FOR i := 1 TO nrData DO BEGIN
  234.             val:=(data[i]-minrange)*range;
  235.             moveto(round(val), 10);
  236.             line(0, 80);
  237.         END;
  238.     END;
  239. END;
  240.  
  241. FUNCTION tToy_drawtoy(PROCEDURE DrawWhatToDraw): PicHandle;
  242. VAR
  243.     myframe: rect;
  244.     p: PicHandle;
  245.     i: integer;
  246.     oldCol: RGBColor;
  247.     val:real;
  248.     minrange,range:real;
  249.     minval,maxval:integer;s:str255;
  250. BEGIN
  251.     IF ToyRec=NIL THEN tToy_drawtoy:=NIL ELSE BEGIN
  252.  
  253.  
  254.         GetForeColor(OldCol);
  255.         RGBForeColor(ToyRec^.col);
  256.         setrect(myFrame, 0, 0, ToyRec^.nrData, 100);
  257.         P := OpenPicture(myframe);
  258.  
  259.         DrawWhatToDraw;
  260.  
  261.         ClosePicture;
  262.         RGBForeColor(Oldcol);
  263.         tToy_drawtoy := p;
  264.     END;{<>nil}
  265. END;
  266. END.